# -*-coding: utf-8 -*-

import cv2
import numpy as np
#import txtProc

winName = "label window"
cv2.namedWindow(winName, cv2.WINDOW_FREERATIO)
cv2.resizeWindow(winName, 720, 720)
cv2.moveWindow(winName, 0, 0)

xyList = []
#ix, iy = -1, -1

def put_text(imgClone):
    cv2.putText(imgClone, "README:", (4, 560), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
    cv2.putText(imgClone, "1.If eye-landmarks not accuracy, please relabel eye-landmarks.", (4, 580), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
    cv2.putText(imgClone, "2.Must be 6 or 12 lds(6 lds per eye).", (4, 600), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
    cv2.putText(imgClone, "3.Begin with left-eye-left-corner, clockwise.", (4, 620), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

    cv2.putText(imgClone, "MOUSE-CLICK: relabel landmark", (4, 660), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 100, 155), 2) 
    cv2.putText(imgClone, "ENTER: finish relabel, next pic", (4, 680), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 100, 155), 2) 
    cv2.putText(imgClone, "ESC: cancel and relabel again", (4, 700), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 100, 155), 2) 


# 创建回调函数
def draw_label(event, x, y, flags, param):
    #global ix, iy, xyList, imgClone
    global xyList, imgClone
    if event == cv2.EVENT_LBUTTONDOWN:
        #ix, iy = x, y
        xyList.append((x, y))
        # 绘制小圆点
        for i in range(len(xyList)):
            cv2.circle(imgClone, xyList[i], 3, (255, 0, 0), -1)


def run(intputImg):#, filePath, imgShape):
    """
    下面把回调函数与OpenCV窗口绑定在一起
    """
    global img, imgClone, xyList
    img = intputImg
    imgClone = img.copy()
    put_text(imgClone)

    cv2.setMouseCallback(winName, draw_label)
    while True:
        cv2.imshow(winName, imgClone)
        k = cv2.waitKey(1)

        if k == 13:  # enter
            xyListBak = xyList.copy()
            xyList = []
            if len(xyListBak) != 0 and len(xyListBak) != 6 and len(xyListBak) != 12:
                print("Must be 6 or 12 lds(6 lds per eye)!")
                imgClone = img.copy()
                put_text(imgClone)
            else:
                return xyListBak

        if k == 27:  # esc, cancel this pic's label and relabel
            xyList = []
            imgClone = img.copy()
            put_text(imgClone)


if __name__ == "__main__":
    print("haha")
